package com.zml.util;

import com.spire.pdf.PdfDocument;
import com.spire.pdf.security.PdfEncryptionKeySize;
import com.spire.pdf.security.PdfPermissionsFlags;

import java.util.EnumSet;

public class EncryptPDF {

    private static String USER_PASS = "Hello123";

    private static String OWNER_PASS = "Owner123";


    public static void EncryptPdf(PdfDocument document, String openPassword) {

        try {
            //PDF
            PdfEncryptionKeySize keySize = PdfEncryptionKeySize.Key_128_Bit;
            //String openPassword = "123456";
           // String permissionPassword = "test";
            
            String permissionPassword = openPassword;
            EnumSet flags = EnumSet.of(PdfPermissionsFlags.Print, PdfPermissionsFlags.Fill_Fields);
            document.getSecurity().encrypt(openPassword, permissionPassword, flags, keySize);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    public static void EncryptPdf(String filePathStr, String openPassword) {

        try {
            //PDF
            PdfDocument doc = new PdfDocument();
            doc.loadFromFile(filePathStr);
            PdfEncryptionKeySize keySize = PdfEncryptionKeySize.Key_128_Bit;
            //String openPassword = "123456";
            //String permissionPassword = "test";
            String permissionPassword = openPassword;
            EnumSet flags = EnumSet.of(PdfPermissionsFlags.Print, PdfPermissionsFlags.Fill_Fields);
            doc.getSecurity().encrypt(openPassword, permissionPassword, flags, keySize);
            doc.saveToFile(filePathStr);
            doc.dispose();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}
